Read five characters at a time until the end of the file is reached.

The following example reads five characters at a time until the end of the file is reached.

C# .NET

public static String DoTest()
{
         string strRet = "";
         string path = @"c:\temp\MyTestRead.txt";

         try
         {
                  if (File.Exists(path))
                  {
                           File.Delete(path);
                  }

                  StreamWriter sw = new StreamWriter(path);
                  sw.WriteLine("This");
                  sw.WriteLine("is some text");
                  sw.WriteLine("to test");
                  sw.WriteLine("Read method");
                  sw.Flush();
                  sw.Close();

                  StreamReader sr = new StreamReader(path);
                  //This is an arbitrary size for this example.
                  char[] c = null;
                  while (sr.Peek() >= 0)
                  {
                           c = new char[5];
                           sr.Read(c, 0, c.Length);
                           //The output will look odd, because
                           //only five characters are read at a time.
                           StringBuilder sb = new StringBuilder();
                           sb.Append(c);
                           strRet += sb.ToString();
                  }
                  sr.Close();
                  
         }
         catch (Exception e)
         {
                  strRet += String.Format("The process failed: {0}", e.ToString());
         }

         return strRet;         
}

 

Blaze++ .NET

static String DoTest()
{
         String strRet = "";
         String path = "c:\\temp\\MyTestRead.txt";

         try
         {
                  if (File::Exists(path))
                  {
                           File::Delete(path);
                  }

                  StreamWriter sw(path);
                  sw.WriteLine("This");
                  sw.WriteLine("is some text");
                  sw.WriteLine("to test");
                  sw.WriteLine("Read method");
                  sw.Close();

                  StreamReader sr(path);
                  //This is an arbitrary size for this example.
                  while (sr.Peek() >= 0)
                  {
                           charA c(5);
                           sr.Read(c, 0, c.Length);
                           //The output will look odd, because
                           //only five characters are read at a time.
                           StringBuilder sb;
                           sb.Append(c);
                           strRet += sb.ToString();
                  }
                  sr.Close();
                  
         }
         catch (Exception e)
         {
                  strRet += String::Format("The process failed: {0}", e.ToString());
         }

         return strRet;         
}